home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Personal Computer World 2009 February
/
PCWFEB09.iso
/
Software
/
Resources
/
Browsers, Managers & Extensions
/
Firefox Backup Extension 6.0.3
/
chrome
/
febe.jar
/
content
/
boxnet.js
< prev
next >
Wrap
Text File
|
2008-09-22
|
18KB
|
502 lines
// author: Chuck Baker
// contact: febe@customsoftwareconsult.com
// Version 6.0
var febeBoxKey = "nyusp9iniip8inoiau41uay4ygttlsku";
var febeAuthToken, febeFolderID, febeBoxMessages = [];
function febeUploadBackup(){
febeMsg = window.arguments[0]; // Load messages
window.document.title = febeMsg[238];
//window.addEventListener("close",febeUploadClose,true);
//window.addEventListener("unload",febeUploadClose,true);
febeUploadAborted = false;
var results = febeBoxLogin();
if(results == false)return false;
var resStatus = febeParseResp(results,"status");
if(resStatus != "logged"){
var tmp="<style>color: red; font-weight: bold; font-size: 15px;</style>"+febeMsg[186]+"\n"+ resStatus;
febeAlert(tmp);
febeUploadAborted = true;
return false;
}//if
//febeAuthToken = febeParseResp(results,"auth_token");
febeGetBoxMsg(results);
try{
var tmp="<style>color: green; font-weight: bold; font-size: 15px;</style>"+febeMsg[185];
febeprt(febeMsg[185],"green");
febeprt(febeBoxMessages[0],"blue");
febeprt(febeBoxMessages[1],"blue");
febeprt(febeBoxMessages[2],"blue");
febeprt(febeBoxMessages[3],"blue");
febeprt(febeBoxMessages[4],"blue");
febeprt(febeBoxMessages[5],"blue");
febeprt(febeBoxMessages[6],"blue");
febeStartUploadBackup()
}catch(e){
var tmp="<style>color: red; font-weight: bold; font-size: 15px;</style>"+febeMsg[8]+"\n"+e;
febeAlert(tmp);
}//try/catch
var box = document.getElementById("messages");
box.contentDocument.close();
febeUploadClose();
return !febeUploadAborted;
}//febeUploadBackup()
function febeStartUploadBackup(){
var prefName = "extensions.febe.lastbackup.folder";
var lastBUfolder = febeGetUnicharPref(prefName);
if(lastBUfolder == ""){
febeAlert(febeMsg[9]);
window.close();
return false;
}//if
febeBuDesDir = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
febeBuDesDir.initWithPath(lastBUfolder);
febeprt("<br>"+febeMsg[10]+" "+lastBUfolder,"purple");
febeCreateBoxFolder();
var errCnt = 0;
var upCnt = 0;
var entries = febeBuDesDir.directoryEntries;
while(entries.hasMoreElements()){
var entry = entries.getNext();
entry.QueryInterface(Ci.nsILocalFile);
if(entry.isDirectory()) continue; // Skip folders
if(febeUpload(entry)){
upCnt++;
}else{
errCnt++;
}//if
}//while
if(errCnt == 0){
var tmp = febeMsg[11]+" "+upCnt+" "+febeMsg[26];
febeprt(tmp,"purple");
}else{
var tmp = "<style>color: red; font-weight: bold; font-size: 15px;</style>";
tmp += febeMsg[20];
}//if
febeAlert(tmp);
return true;
}//febeStartUploadBackup()
function febeCreateBoxFolder(){
// Create the backup folder
var folderName = "FEBE backups"
var xmlstr = "<?xml version='1.0' encoding='UTF-8' ?>";
xmlstr += "<request>";
xmlstr += "<action>create_folder</action>";
xmlstr += "<api_key>"+febeBoxKey+"</api_key>";
xmlstr += "<auth_token>"+febeAuthToken+"</auth_token>";
xmlstr += "<parent_id>0</parent_id>";
xmlstr += "<name>"+folderName+"</name>";
xmlstr += "<share>0</share>"; // Set to "1" to publicly share
xmlstr += "</request>";
var req = new XMLHttpRequest();
req.open('POST', 'http://www.box.net/api/1.0/xml', false);
req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
//req.send(xmlstr);
try{
req.send(xmlstr);
}catch(e){
febeUploadAborted = true;
return false;
}//try/catch
var resp = req.responseText;
var resStatus = febeParseResp(resp,"status");
var folderName = febeParseResp(resp,"folder_name");
febeFolderID = febeParseResp(resp,"folder_id")
switch(resStatus){
case "create_ok":
febeprt(febeMsg[121]+" "+folderName, "green");
break;
case "s_folder_exists":
febeprt(febeMsg[122]+" "+folderName+ " OK ", "orange");
break;
default:
febeAlert(febeMsg[123]+" " + resStatus);
return false;
}//switch
/*
// Create subfolder
var folderName = "FEBE 2008 06-07 05.19.35"
var xmlstr = "<?xml version='1.0' encoding='UTF-8' ?>";
xmlstr += "<request>";
xmlstr += "<action>create_folder</action>";
xmlstr += "<api_key>"+febeBoxKey+"</api_key>";
xmlstr += "<auth_token>"+febeAuthToken+"</auth_token>";
xmlstr += "<parent_id>"+febeFolderID+"</parent_id>";
xmlstr += "<name>"+folderName+"</name>";
xmlstr += "<share>0</share>"; // Set to "1" to publicly share
xmlstr += "</request>";
var req = new XMLHttpRequest();
req.open('POST', 'http://www.box.net/api/1.0/xml', false);
req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
//req.send(xmlstr);
try{
req.send(xmlstr);
}catch(e){
febeUploadAborted = true;
return false;
}//try/catch
var resp = req.responseText;
var resStatus = febeParseResp(resp,"status");
switch(resStatus){
case "create_ok":
var folderName = febeParseResp(resp,"folder_name")
febeFolderID = febeParseResp(resp,"folder_id")
febeprt("Created folder: "+folderName+ " ID: "+febeFolderID, "green");
break;
case "s_folder_exists":
var febeFolderID = febeParseResp(resp,"folder_id")
febeprt("Folder already exists: "+folderName+ " ID: "+febeFolderID, "orange");
break;
default:
alert(febeMsf[123] + resStatus);
}//switch
*/
return true;
}//febeCreateBoxFolder()
function febeUpload(nsFilename){
var uploadName = nsFilename.leafName;
febeprt("<br>"+febeMsg[21]+" "+uploadName,"purple")
// Buffer the upload file
var inStream = Cc["@mozilla.org/network/file-input-stream;1"].createInstance(Ci.nsIFileInputStream);
inStream.init(nsFilename, 1, 1, inStream.CLOSE_ON_EOF);
var bufInStream = Cc["@mozilla.org/network/buffered-input-stream;1"].createInstance(Ci.nsIBufferedInputStream);
bufInStream.init(inStream, 4096);
//Setup the boundry start stream
var boundary = "--F-E-B-E--U-p-l-o-a-d-------------" + Math.random();
var startBoundryStream = Cc["@mozilla.org/io/string-input-stream;1"].createInstance(Ci.nsIStringInputStream);
startBoundryStream.setData("\r\n--"+boundary+"\r\n",-1);
// Setup the boundry end stream
var endBoundryStream = Cc["@mozilla.org/io/string-input-stream;1"].createInstance(Ci.nsIStringInputStream);
endBoundryStream.setData("\r\n--"+boundary+"--",-1);
// Setup the mime-stream - the 'part' of a multi-part mime type
var mimeStream = Cc["@mozilla.org/network/mime-input-stream;1"].createInstance(Ci.nsIMIMEInputStream);
mimeStream.addContentLength = false;
mimeStream.addHeader("Content-disposition","form-data; name=\""+uploadName+"\"; filename=\""+uploadName+"\"");
mimeStream.addHeader("Content-type","multipart/form-data");
mimeStream.setData(bufInStream);
// Setup a multiplex stream
var multiStream = Cc["@mozilla.org/io/multiplex-input-stream;1"].createInstance(Ci.nsIMultiplexInputStream);
multiStream.appendStream(startBoundryStream);
multiStream.appendStream(mimeStream);
multiStream.appendStream(endBoundryStream);
var size = Math.round(nsFilename.fileSize / 1024);
febeprt("Uploading: "+uploadName+" "+size+"kb");
var uri = 'http://upload.box.net/api/1.0/upload/'+febeAuthToken+'/'+febeFolderID;
var req = new XMLHttpRequest();
req.onprogress = onProgress;
req.open('POST', uri, false);
req.setRequestHeader("Content-length",multiStream.available());
req.setRequestHeader("Content-type","multipart/form-data; boundary="+boundary);
//req.send(multiStream);
try{
req.send(multiStream);
}catch(e){
febeUploadAborted = true;
return false;
}//try/catch
var resp = req.responseText;
var resStatus = febeParseResp(resp,"status");
switch(resStatus){
case "upload_ok":
febeprt(uploadName+" uploaded ok","green");
break;
case "upload_some_files_failed":
var resp = req.responseText;
var domParser = new DOMParser();
var xmldoc = domParser.parseFromString(resp, "text/xml");
var response = xmldoc.getElementsByTagName('response')[0];
var files = response.getElementsByTagName("files")[0].firstChild
var err = files.getAttribute("error");
var fname = files.getAttribute("file_name");
febeprt(febeMsg[124]+" "+fname+": "+err+febeMsg[105], "red");
return false;
break;
default:
febeAlert(req.responseText);
return false;
}//switch
return true;
}//febeUpload()
function febeprt(txt,color){
if(!color) color="black";
var box = document.getElementById("messages");
txt = "<span style='font-family: Courier New,Courier,monospace; font-size: 12px; color: "+color+";'>"+txt+"<br></span>"
box.contentDocument.write(txt);
var scrollBox = box.contentWindow.document.body;
scrollBox.scrollTop = scrollBox.scrollHeight - scrollBox.clientHeight
return true;
}//febeprt()
function febeParseResp(resp,which){
var domParser = new DOMParser();
var xmldoc = domParser.parseFromString(resp, "text/xml");
var value;
try{
value = xmldoc.getElementsByTagName(which)[0].firstChild.nodeValue;
}catch(e){value = false;}
return value;
}//febeParseResp
function onProgress(e) {
var percentComplete = (e.position / e.totalSize)*100;
return
}
function febeTestBN(){
// Test Box.net login -
febeSetMsgs();
var results = febeBoxLogin();
if(results == false)return false;
var resStatus = febeParseResp(results,"status");
if(resStatus != "logged"){
var tmp="<style>color: red; font-weight: bold; font-size: 15px;</style>"+febeMsg[186]+"\n"+ resStatus;
febeAlert(tmp);
return false;
}//if
febeGetBoxMsg(results);
var tmp="<style>color: green; font-weight: bold; font-size: 15px;</style>"+febeMsg[185];
tmp += "\n"+febeBoxMessages[0]+"\n";
tmp += "\n"+febeBoxMessages[1];
tmp += "\n"+febeBoxMessages[2];
tmp += "\n"+febeBoxMessages[3];
tmp += "\n"+febeBoxMessages[4];
tmp += "\n"+febeBoxMessages[5];
tmp += "\n"+febeBoxMessages[6];
febeAlert(tmp);
febeBoxLogout();
return true;
}//febeTestBN()
function febeGetBoxMsg(xml){
febeBoxMessages = [];
var login = febeParseResp(xml,"login");
var email = febeParseResp(xml,"email");
var user_id = febeParseResp(xml,"user_id");
var capacity = febeParseResp(xml,"space_amount");
var space_used = febeParseResp(xml,"space_used");
var max_upload_size = febeParseResp(xml,"max_upload_size");
febeBoxMessages.push(febeMsg[192]+" "+login);
febeBoxMessages.push(febeMsg[193]+" "+email);
febeBoxMessages.push(febeMsg[194]+" "+user_id);
var mb = 1048576;
var boxCapacity = parseInt(capacity) / mb;
boxCapacity = Math.round(boxCapacity * 100);
boxCapacity = parseInt(boxCapacity) / 100;
var spaceUsed = parseInt(space_used) / mb;
spaceUsed = Math.round(spaceUsed * 100);
spaceUsed = parseInt(spaceUsed) / 100;
var spaceAvailable = boxCapacity - spaceUsed;
var maxUpload = parseInt(max_upload_size) / mb;
febeBoxMessages.push(febeMsg[195]+" "+boxCapacity+" MB");
febeBoxMessages.push(febeMsg[196]+" "+spaceUsed+" MB");
febeBoxMessages.push(febeMsg[197]+" "+spaceAvailable+" MB");
febeBoxMessages.push(febeMsg[198]+" "+maxUpload+" MB");
return true;
}//febeGetBoxMsg()
function febeLoginMgrObj(username, password){
this.hostname = 'chrome://FEBE {Box.net login}';
this.formSubmitURL = 'Box.net login';
this.httprealm = null;
this.username = username;
this.password = password;
var nsLoginInfo = new Components.Constructor("@mozilla.org/login-manager/loginInfo;1", Components.interfaces.nsILoginInfo, "init");
return new nsLoginInfo(this.hostname, this.formSubmitURL, this.httprealm, this.username, this.password, "", "");
}//febeLoginMgrObj()
function febeGetBNpassword(username){
// Return box.net password or null
var loginManager = Cc["@mozilla.org/login-manager;1"].getService(Ci.nsILoginManager);
var password;
var login = febeLoginMgrObj(username, password)
try {
var logins = loginManager.findLogins({}, login.hostname, login.formSubmitURL, login.httprealm);
// Find user from returned array of nsILoginInfo objects
for (var i = 0; i < logins.length; i++) {
if (logins[i].username == username) {
password = logins[i].password;
return password;
break;
}//if
}//for
} catch(ex) {
return null;
}//try
}//febeGetBNpassword()
function febeDisableBoxCheck(){
var enabled = document.getElementById("box_net_enable").checked;
document.getElementById("box_net_info").hidden = !enabled;
var prefName = "extensions.febe.boxnet.username";
var uName = febePrefs.getCharPref(prefName);
document.getElementById("bn_password").value = febeGetBNpassword(uName);
var prefName = "extensions.febe.lastbackup";
var tmp = febePrefs.getCharPref(prefName);
var lastBU = false;
if(tmp != "") lastBU = true;
if(uName != ""){ // Disable entry if it already exists - Enabled with "Reset login" button
document.getElementById("bn_username").disabled = true;
document.getElementById("bn_password").disabled = true;
document.getElementById("bn_save").disabled = true;
document.getElementById("bn_test").disabled = false;
document.getElementById("bn_upload").disabled = !lastBU;
document.getElementById("bn_view").disabled = false;
document.getElementById("box_net_autoupload").disabled = false;
}else{
document.getElementById("bn_test").disabled = true;
document.getElementById("bn_upload").disabled = true;
document.getElementById("bn_view").disabled = true;
document.getElementById("bn_save").disabled = false;
document.getElementById("box_net_autoupload").disabled = true;
document.getElementById("box_net_autoupload").checked = false;
document.getElementById("bn_password").value = "";
}//if
return true;
}//febeDisableBoxCheck()
function febeUploadAbort(){
var tmp = "<style>color: red; font-weight: bold; font-size: 15px;</style>";
tmp += febeMsg[8];
febeAlert(tmp);
return true;
}//febeUploadAbort()
function febeUploadClose(){
if(febeUploadAborted) febeUploadAbort();
febeBoxLogout();
return true;
}//febeUploadClose()
function febeBoxLogout(){
var req = new XMLHttpRequest();
var str = 'http://www.box.net/api/1.0/rest?action=logout&api_key='+febeBoxKey+'&auth_token='+febeAuthToken;
req.open('GET',str, false);
req.send(null);
var resp = req.responseText;
var resStatus = febeParseResp(resp,"status");
if (resStatus != "logout_ok"){
var tmp="<style>color: red; font-weight: bold; font-size: 15px;</style>"+febeMsg[190]+"\n"+ febeMsg[189]+" "+resStatus;
febeAlert(tmp);
return false;
}//if
return true;
}//febeBoxLogout()
function febeResetBN(){
febeClearLogin()
document.getElementById("bn_username").disabled = false;
document.getElementById("bn_username").value = "";
document.getElementById("bn_password").disabled = false;
document.getElementById("bn_password").value = "";
document.getElementById("bn_test").disabled = true;
document.getElementById("bn_upload").disabled = true;
document.getElementById("bn_view").disabled = true;
document.getElementById("bn_save").disabled = false;
document.getElementById("box_net_autoupload").disabled = true;
document.getElementById("box_net_autoupload").checked = false;
febePrefs.setBoolPref("extensions.febe.boxnet.autoupload",false);
febePrefs.setCharPref("extensions.febe.boxnet.username","");
febePrefs.setCharPref("extensions.febe.boxnet.password","");
document.getElementById("bn_username").focus();
}//febeResetBN()
function febeSaveBN(){
var uName = document.getElementById("bn_username").value;
var password = document.getElementById("bn_password").value;
if(password == "" || uName == ""){
febeAlert(febeMsg[187]);
if(uName == "") document.getElementById("bn_username").focus();
if(password == "") document.getElementById("bn_password").focus();
if(password == "" || uName == "") document.getElementById("bn_username").focus();
return false;
}//if
var loginInfo = new febeLoginMgrObj(uName, password);
Cc["@mozilla.org/login-manager;1"].getService(Ci.nsILoginManager).addLogin(loginInfo);
febePrefs.setCharPref("extensions.febe.boxnet.username",uName);
febePrefs.setCharPref("extensions.febe.boxnet.password","<encrypted>");
document.getElementById("bn_test").disabled = false;
document.getElementById("bn_upload").disabled = false;
document.getElementById("bn_view").disabled = false;
document.getElementById("bn_save").disabled = true;
document.getElementById("box_net_autoupload").disabled = false;
return true;
}//febeSaveBN()
function febeClearLogin(){
// Remove all FEBE box.net logins
var hostname = 'chrome://FEBE {Box.net login}';
var formSubmitURL = 'Box.net login';
var httprealm = null;
var passwordManager = Cc["@mozilla.org/login-manager;1"].getService(Ci.nsILoginManager);
var logins = passwordManager.findLogins({}, hostname, formSubmitURL, httprealm);
for (var i = 0; i < logins.length; i++) {
if (logins[i].hostname == hostname) {
passwordManager.removeLogin(logins[i]);
break;
}//if
}//for
return true;
}//febeClearLogin()
function febeViewBoxFolder(){
febeBoxLogin();
febeOpenLink('http://www.box.net/files');
return true;
}//febeViewBoxFolder()
function febeBoxLogin(){
var prefName = "extensions.febe.boxnet.username";
var bnUsername = febePrefs.getCharPref(prefName);
var bnPassword = febeGetBNpassword(bnUsername);
var method = "";
var k = "&api_key=" + febeBoxKey;
var l = "&login=" + encodeURIComponent(bnUsername);
var p = "&password=" + encodeURIComponent(bnPassword);
var m = "&method=";
var req = new XMLHttpRequest();
req.open('GET', 'http://www.box.net/api/1.0/rest?action=authorization' + k + l + p + m, false);
req.onreadystatechange = function (aEvt) {
var r = req.readyState;
var s = req.status;
if (req.readyState != 4 || req.status != 200) {
var tmp="<style>color: red; font-weight: bold; font-size: 15px;</style>"+febeMsg[188]+"\n"+ febeMsg[189]+" "+resStatus;
febeAlert(tmp);
return false;
}//if
};
req.send(null);
var results = req.responseText;
febeAuthToken = febeParseResp(results,"auth_token");
return results;
}//febeBoxLogin()